package com.wuhenzhizao.sku.view;

import com.wuhenzhizao.sku.ResourceTable;
import com.wuhenzhizao.sku.bean.SkuAttribute;
import com.wuhenzhizao.sku.utils.ScreenUtils;
import com.wuhenzhizao.sku.utils.ViewUtils;
import com.wuhenzhizao.sku.widget.FlowLayout1;
import ohos.agp.components.AttrSet;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;

/**
 * 自定义单个Sku布局
 *
 * @author wuhenzhizao
 * @since 2017-08-02
 */
public class SkuItemLayout extends DirectionalLayout {
    private Text attributeNameTv;
    private FlowLayout1 attributeValueLayout;
    private OnSkuItemSelectListener listener;

    /**
     * 构造
     *
     * @param context
     */
    public SkuItemLayout(Context context) {
        this(context,null);
        init(context);
    }

    /**
     * 构造
     *
     * @param context
     * @param attrSet
     */
    public SkuItemLayout(Context context, AttrSet attrSet) {
        this(context, attrSet,"");
    }

    /**
     * 构造
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public SkuItemLayout(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    private void init(Context context) {
        setOrientation(VERTICAL);

        attributeNameTv = new Text(context);
        attributeNameTv.setId(ViewUtils.generateViewId(attributeNameTv));
        attributeNameTv.setTextColor(new Color(Color.getIntColor("#333333")));
        attributeNameTv.setTextSize(14, Text.TextSizeType.FP);
        attributeNameTv.setPaddingForText(false);
        DirectionalLayout.LayoutConfig layoutConfig1 = new DirectionalLayout.LayoutConfig(
                LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
        layoutConfig1.setMarginLeft(ScreenUtils.dp2PxInt(context, 15));
        layoutConfig1.setMarginTop(ScreenUtils.dp2PxInt(context, 15));
        attributeNameTv.setLayoutConfig(layoutConfig1);
        addComponent(attributeNameTv);

        attributeValueLayout = new FlowLayout1(context);
        attributeValueLayout.setFlow(true);
        attributeValueLayout.setId(ViewUtils.generateViewId(attributeValueLayout));
        // 设置流布局高度
        attributeValueLayout.setMinHeight(ScreenUtils.dp2PxInt(context, 60));
        attributeValueLayout.setChildSpacing(ScreenUtils.dp2PxInt(context, 0));
        attributeValueLayout.setRowSpacing(ScreenUtils.dp2PxInt(context, 5));
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(
                LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_CONTENT);
        layoutConfig.setMarginLeft(ScreenUtils.dp2PxInt(context, 0));
        layoutConfig.setMarginTop(ScreenUtils.dp2PxInt(context, 0));
        layoutConfig.setMarginRight(ScreenUtils.dp2PxInt(context, 0));
        layoutConfig.setMarginBottom(ScreenUtils.dp2PxInt(context, 0));
        attributeValueLayout.setLayoutConfig(layoutConfig);
        addComponent(attributeValueLayout);

        Component line = new Component(context);
        ShapeElement shapeElement = new ShapeElement(getContext(),ResourceTable.Graphic_sku_item1);
        shapeElement.setShape(ShapeElement.RECTANGLE);
        line.setBackground(shapeElement);
        DirectionalLayout.LayoutConfig layoutConfig2 = new DirectionalLayout.LayoutConfig(
                LayoutConfig.MATCH_PARENT, 1);
        layoutConfig2.setMarginLeft(ScreenUtils.dp2PxInt(context, 15));
        layoutConfig2.setMarginRight(ScreenUtils.dp2PxInt(context, 15));
        layoutConfig2.setMarginTop(ScreenUtils.dp2PxInt(context, 5));
        LayoutConfig lineParams = new LayoutConfig(layoutConfig2);
        line.setLayoutConfig(lineParams);
        addComponent(line);
    }

    public void setListener(OnSkuItemSelectListener listener) {
        this.listener = listener;
    }

    //    添加其他属性布局（颜色、尺码等）
    public void buildItemLayout(int position, String attributeName, List<String> attributeValueList) {
        attributeNameTv.setText(attributeName);
        attributeValueLayout.removeAllComponents();
        for (int i = 0; i < attributeValueList.size(); i++) {
            SkuItemView itemView = new SkuItemView(getContext());
            itemView.setId(ViewUtils.generateViewId(itemView));
            itemView.setAttributeValue(attributeValueList.get(i));
            itemView.setClickedListener(new ItemClickListener(position, itemView));
            LayoutConfig layoutConfig = new LayoutConfig(
                    LayoutConfig.MATCH_CONTENT,
                    ScreenUtils.dp2PxInt(getContext(), 25));
            layoutConfig.setMarginLeft(ScreenUtils.dp2PxInt(getContext(), 10));
            layoutConfig.setMarginTop(ScreenUtils.dp2PxInt(getContext(), 5));
            layoutConfig.setMarginRight(ScreenUtils.dp2PxInt(getContext(), 10));
            layoutConfig.setMarginBottom(ScreenUtils.dp2PxInt(getContext(), 5));

            itemView.setLayoutConfig(layoutConfig);
            attributeValueLayout.addComponent(itemView);
        }
    }

    /**
     * 清空是否可点击，选中状态
     */
    public void clearItemViewStatus() {
        for (int i = 0; i < attributeValueLayout.getChildCount(); i++) {
            SkuItemView itemView = (SkuItemView) attributeValueLayout.getComponentAt(i);
            ShapeElement shapeElement = new ShapeElement(getContext(),ResourceTable.Graphic_sku_item3);
            shapeElement.setShape(ShapeElement.RECTANGLE);
            itemView.setBackground(shapeElement);
            itemView.setTextColor(new Color(Color.getIntColor("#c1c1c1")));
            itemView.setSelected(false);
            itemView.setEnabled(false);
        }
    }

    /**
     * 设置指定属性为可点击状态
     *
     * @param attributeValue
     */
    public void optionItemViewEnableStatus(String attributeValue) {
        for (int i = 0; i < attributeValueLayout.getChildCount(); i++) {
            SkuItemView itemView = (SkuItemView) attributeValueLayout.getComponentAt(i);
            if (attributeValue.equals(itemView.getAttributeValue())) {
                ShapeElement shapeElement = new ShapeElement(getContext(),ResourceTable.Graphic_sku_item2);
                shapeElement.setShape(ShapeElement.RECTANGLE);
                itemView.setBackground(shapeElement);
                itemView.setTextColor(Color.BLACK);
                itemView.setEnabled(true);
            }
        }
    }

    /**
     * 设置指定属性为选中状态
     *
     * @param selectValue
     */
    public void optionItemViewSelectStatus(SkuAttribute selectValue) {
        for (int i = 0; i < attributeValueLayout.getChildCount(); i++) {
            SkuItemView itemView = (SkuItemView) attributeValueLayout.getComponentAt(i);
            if (selectValue.getValue().equals(itemView.getAttributeValue())) {
                ShapeElement shapeElement = new ShapeElement(getContext(),ResourceTable.Graphic_sku_item1);
                shapeElement.setShape(ShapeElement.RECTANGLE);
                itemView.setBackground(shapeElement);
                itemView.setTextColor(Color.WHITE);
                itemView.setEnabled(true);
                itemView.setSelected(true);
            }
        }
    }

    /**
     * 当前属性集合是否有选中项
     *
     * @return boolean
     */
    @Override
    public boolean isSelected() {
        for (int i = 0; i < attributeValueLayout.getChildCount(); i++) {
            SkuItemView itemView = (SkuItemView) attributeValueLayout.getComponentAt(i);
            if (itemView.isSelected()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取属性名称
     *
     * @return String
     */
    public String getAttributeName() {
        return attributeNameTv.getText().toString();
    }

    private void onSkuItemClicked(int position, SkuItemView view) {
        boolean selected = !view.isSelected();
        SkuAttribute attribute = new SkuAttribute();
        attribute.setKey(attributeNameTv.getText());
        attribute.setValue(view.getAttributeValue());
        listener.onSelect(position, selected, attribute);
    }

    /**
     * item监听
     *
     * @since 2017-08-02
     */
    private class ItemClickListener implements ClickedListener {
        private int position;
        private SkuItemView view;

        ItemClickListener(int position, SkuItemView view) {
            this.position = position;
            this.view = view;
        }

        @Override
        public void onClick(Component component) {
            onSkuItemClicked(position, view);
        }
    }

    /**
     * sku监听
     *
     * @since 2017-08-02
     */
    interface OnSkuItemSelectListener {
        void onSelect(int position, boolean select, SkuAttribute attribute);
    }
}
